<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
<div class="container">
    <h1>留言板</h1>
    <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
    <div class="row">
        <span>谁:</span> <label for="from"> </label><input type="text" name="" id="from">
    </div>
    <div class="row">
        <span>对谁:</span> <label for="to"> </label><input type="text" name="" id="to">
    </div>
    <div class="row">
        <span>说什么:</span> <label for="say"> </label><input type="text" name="" id="say">
    </div>
    <input type="button" value="提交" id="submit" onclick="submit()">
    <!-- <div>A 对 B 说: hello</div> -->
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
    //获取后端的list
    $.ajax({
        url:"/messageWall/getList",
        type:"get",
        success:function (list){
            let finalHTML = "";
            for(const curMessage of list){
                finalHTML = '<div>'+ curMessage.from + ' 对 ' + curMessage.to + ' 说  ' + curMessage.say +'</div>'
            }
            //將信息显示在前端
            $(".container").append(finalHTML);
        }
    })
    function submit(){
        alert("提交成功");
        //1. 获取留言的内容
        const HTML_from = $('#from').val();
        const HTML_to = $('#to').val();
        const HTML_say = $('#say').val();
        console.log("发布留言-> " + HTML_from + "对" + HTML_to + "说:" + HTML_say);
        if (HTML_from=== '' || HTML_to === '' || HTML_say === '') {
            return;
        }
        $.ajax({
            url:"/messageWall/publish",
            type: "post",
            //将前端文本框中的内容保存到后端的list中
            data:{
                //冒号前面的参数是后端的
                //冒号后面的参数是前端的
                from:HTML_from,
                to:HTML_to,
                say:HTML_say,
            },
            //接收后端返回的参数
            success:function (response){
                if(response === true){
                    //2. 构造节点
                    const divE = "<div>" + HTML_from + "对" + HTML_to + "说:" + HTML_say + "</div>";
                    //3. 把节点添加到页面上
                    $(".container").append(divE);
                    //4. 清空输入框的值
                    $('#from').val("");
                    $('#to').val("");
                    $('#say').val("");
                }else {
                    alert("输入错误");
                }
            }
        })
    }

</script>
</body>

</html>